import requestInstance from "@/utils/requestInstance.ts";
import {
  CreateDraftRes,
  ResType,
  DraftDetail,
  UploadImageRes,
  SaveDraftRes,
} from "./shared";

/**
 * 获取草稿
 * token: 
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6IjEyMzQ1Njc4OTBAcXEuY29tIiwiaWF0IjoxNzI5NjQ3ODIxLCJleHAiOjE3Mjk3MzQyMjF9.qDiPyrE_8R4XPKouM8kHQGoB2H_S1lorVkgB4QrYlOY
 */
export const getDraftDetail = async (draftId: string) => {
  const res = await requestInstance.post<ResType<DraftDetail>>(
    "/private/getDraft",
    {
      draftId,
    }
  );

  return res.data;
};

/**
 * 用户上传文章图片
 */
export const uploadImageToArticle = async (file: File, articleId: string) => {
  const res = await requestInstance.post<ResType<UploadImageRes>>(
    "/private/uplaodArticleImg",
    {
      articleId,
      file,
    },
    {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    }
  );

  return res.data;
};

/**
 * 新建草稿
 * @param
 * @return
 */
export const createDraft = async () => {
  const res = await requestInstance.get<ResType<CreateDraftRes>>(
    "/private/addDraft"
  );

  return res.data;
};

/**
 * 保存草稿
 * @param
 * @return
 */
export async function saveDraft({
  draftId,
  title,
  summary,
  content,
  createTime,
}: {
  draftId: string;
  title: string;
  summary: string;
  content: string;
  createTime: string;
}) {
  const res = await requestInstance.post<ResType<SaveDraftRes>>(
    "/private/saveDraft",
    {
      draftId,
      title,
      summary,
      content,
      createTime,
    }
  );

  return res.data;
}

/**
 * 删除草稿
 * @param
 * @return
 */
export const deleteDraft = async (draftId: string) => {
  const res = await requestInstance.get<ResType<string>>(
    "/private/deleteDraft",
    {
      params: {
        draftId,
      },
    }
  );

  return res.data;
};
